﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Data;
using Restaurant.DTO;
using Restaurant.DAO;

namespace Restaurant.BUS
{
    public class XuatKhoService
    {
        XuatKhoDB xkDAO = new XuatKhoDB();

        public DataTable LayXuatKho()
        {
            return xkDAO.LayXuatKho();
        }

        public XuatKho LayXuatKhoTheoMa(int maXuatKho)
        {
            DataTable dbtab = xkDAO.LayXuatKhoTheoMa(maXuatKho);
            XuatKho xk = new XuatKho();
            DataRow dbrow = dbtab.Rows[0];
            if (dbrow[0] != null)
                xk.MAXUATKHO = int.Parse(dbrow[0].ToString());
            if (dbrow[1] != null)
                xk.NGAYXUAT = DateTime.Parse(dbrow[1].ToString());
            if (dbrow[2] != null)
                xk.MAKHO = int.Parse(dbrow[3].ToString());
            //thieu chi tiet xuat kho
            return xk;
        }

        public DataTable LayNLXuatKho(int maKho)
        {
            DataTable dbtab = xkDAO.LayNLXuatKho(maKho);
                return dbtab;
        }

        public bool ThemXuatKho(ref XuatKho xk)
        {
            if (xk.MAKHO.ToString() == "" || xk.NGAYXUAT.ToString() == "" )
            {
                return false;
            }
            if (xkDAO.ThemXuatKho(ref xk))
                return true;
            return false;
        }

        public bool ThemCTXuatKho(XuatKho xk)
        {
            bool flag = true;
            for (int i = 0; i < xk.CHITIETXUAT.Rows.Count; i++)
            {
                if (!xkDAO.ThemCTXuatKho(xk.MAXUATKHO, int.Parse(xk.CHITIETXUAT.Rows[i]["MaNL"].ToString()), float.Parse(xk.CHITIETXUAT.Rows[i]["SoLuong"].ToString()), xk.MAKHO))
                {
                    flag = false;
                    
                }
            }
            if (!flag)
            {
                xkDAO.XoaCTXuatKho(xk.MAXUATKHO);
            }
            return flag;
        }

        public bool SuaXuatKho(XuatKho xk)
        {
            if (xkDAO.SuaXuatKho(xk))
                return true;
            return false;
        }

        public bool XoaXuatKho(int ma)
        {
            if (xkDAO.XoaXuatKho(ma))
                return true;
            return false;
        }

        public bool ThemCTXuatKhoDeadlockError(XuatKho xk)
        {
            bool flag = true;
            for (int i = 0; i < xk.CHITIETXUAT.Rows.Count; i++)
            {
                if (!xkDAO.ThemCTXuatKhoDeadlockError(xk.MAXUATKHO, int.Parse(xk.CHITIETXUAT.Rows[i]["MaNL"].ToString()), float.Parse(xk.CHITIETXUAT.Rows[i]["SoLuong"].ToString()), xk.MAKHO))
                {
                    flag = false;

                }
            }
            if (!flag)
            {
                xkDAO.XoaCTXuatKho(xk.MAXUATKHO);
            }
            return flag;
        }

        public bool ThemCTXuatKhoDeadlock(XuatKho xk)
        {
            bool flag = true;
            for (int i = 0; i < xk.CHITIETXUAT.Rows.Count; i++)
            {
                if (!xkDAO.ThemCTXuatKhoDeadlock(xk.MAXUATKHO, int.Parse(xk.CHITIETXUAT.Rows[i]["MaNL"].ToString()), float.Parse(xk.CHITIETXUAT.Rows[i]["SoLuong"].ToString()), xk.MAKHO))
                {
                    flag = false;

                }
            }
            if (!flag)
            {
                xkDAO.XoaCTXuatKho(xk.MAXUATKHO);
            }
            return flag;
        }
    }
}
